package gameAppLayer;

import java.util.HashMap;

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioData;
import com.jme3.audio.AudioNode;
import com.jme3.material.Material;
import com.jme3.texture.Texture;

public class AssetPortal {
	
	private static AssetManager assetManager;
	private static HashMap<String, Material> materialMap = new HashMap<String, Material>();	
	private static HashMap<String, AudioNode> audioMap = new HashMap<String, AudioNode>();
	
	public AssetPortal(AssetManager assetManager) {
		AssetPortal.assetManager = assetManager;
	}
	
	/**
	 * Load into cache, returning reference to loaded object
	 * @param name
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Object getAsset(String name) {		
		return assetManager.loadAsset(name);
	}
	
	/**
	 * Load into cache without returning the object
	 * @param name
	 */
	@SuppressWarnings("rawtypes")
	public static void loadAsset(String name) {
		assetManager.loadAsset(name);		
	}
	
	public static Texture loadTexture(String name) {
		return assetManager.loadTexture(name);
	}
	
	/**
	 * Load .j3m file from external source file into assetManager cache and into memory here.
	 * @param name
	 * @return
	 */
	public static Material loadMaterialFile(String name) {
		Material mat = assetManager.loadMaterial(name);
		materialMap.put(name, mat);
		return mat;
	}
	
	/**
	 * Add run-time configured material to storage here.  Material already loaded into AssetManager cache
	 * during its creation.
	 * @param name
	 * @param material
	 */
	public static void addMaterial(String name, Material material) {
		materialMap.put(name, material);
	}
	
	/**
	 * Retrieve run-time configured material from storage here.
	 * @param name
	 * @return
	 */
	//QQQ:  What if the material doesn't exist?  Should automatically fetch some default material, based on input
	public static Material getMaterial(String name) {
		return materialMap.get(name);
	}
	
	/**
	 * Used to construct an AudioNode.  Loads data into the AssetManager, returning the 
	 * generated AudioData class.
	 * @param filePath
	 * @return
	 */
	public static AudioData getAudioData(String filePath) {
		return assetManager.loadAudio(filePath);
	}
	
	public static void addAudioNode(String audioEventName, AudioNode node){
		audioMap.put(audioEventName, node);
	}
	
	public static void removeAudioNode(String audioEventName) {
		audioMap.remove(audioEventName);
	}
	
	//QQQ:  What if audioNode doesn't exist?  Need to handle error
	public static AudioNode getAudioNode(String audioEventName) {
		AudioNode temp = audioMap.get(audioEventName);
		//make a copy of the node rather than return it directly.  Can only attach to one parent at a time.
		return temp.clone(); 
	}
}
